﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Godot;
using kemocard.Scripts.Common;
using kemocard.Scripts.Module.Battle;
using kemocard.Scripts.MVC;
using kemocard.Scripts.Pawn;

namespace kemocard.Scripts.Buff;

public class BaseBuff : ICloneable
{
    // Buff唯一标识
    public string Id = "";
    public string Name = "";
    public string Description = "";
    public int Duration;
    public BuffType Type = BuffType.NONE;
    public int StackNum = 0;
    public int StackLimit = 0;
    public bool IsUnique = true;
    public string HashCode = "";
    public readonly HashSet<BuffTag> Tags = [];

    /**
     * 预留一个临时变量用于存储数据，具体使用看卡牌脚本
     */
    public float TempValue = 0f;

    /// <summary>
    /// 预留三个特殊标记，用于记录卡牌打出的时候是否触发了特效
    /// </summary>
    public bool SpecialFlag = false;
    public bool SpecialFlag2 = false;
    public bool SpecialFlag3 = false;

    /**
     * Mod可能为null,使用时注意
     */
    protected readonly BattleModel Mod = GameCore.ControllerMgr.GetControllerModel<BattleModel>(ControllerType.Battle);

    public BasePawn Owner;
    public BasePawn Causer;

    /// <summary>
    /// 在设置Owner后执行，此时持有者和目标都已经被设置
    /// </summary>
    public virtual void AfterSettingOwner()
    {
    }

    public virtual void RefreshProps(ref TempPropStruct inStruct)
    {
    }

    public virtual Task ApplyBuff(ref object data, BuffTag tag = BuffTag.None)
    {
        return Task.CompletedTask;
    }

    public virtual void OnRemoved()
    {
        GameCore.EventBus.RemoveObjAllEvents(this);
    }

    ~BaseBuff()
    {
        GameCore.EventBus.RemoveObjAllEvents(this);
    }

    public object Clone()
    {
        return MemberwiseClone();
    }

    public virtual void CountDuration()
    {
        Duration--;
        if (Duration > 0) return;
        Owner?.RemoveBuff(IsUnique ? HashCode : Id, false, StackNum);

        Owner = null;
        Causer = null;
    }
}

public enum BuffType
{
    NONE,
    BUFF,
    DEBUFF
}

public enum BuffTag
{
    None,

    /** 增加最大HP 加区 */
    AddHealth,

    /** 增加最大HP 乘区 */
    ExtraHealth,
    AddPAttack,
    ExtraPAttack,
    AddMAttack,
    ExtraMAttack,
    AddPDefense,
    ExtraPDefense,
    AddMDefense,
    ExtraMDefense,

    /** 增加回复量 */
    AddHeal,

    /** 增加回复量 乘区 */
    ExtraHeal,
    AddDraw,

    /** 伤害结算前 */
    BeforeAttacked,

    /** 伤害结算后 */
    Attacked,
    Attack,
    TurnEnd,
    TurnStart,
    BattleStart,

    /** 释放回复效果 */
    Heal,

    /** 被治疗 */
    Healed,

    /** 嘲讽 */
    Taunt,

    /** 死亡前 */
    BeforeDead,

    /** 造成水属性伤害加成 */
    WaterUp,

    /** 受到水属性伤害增加 */
    BeWaterUp,

    /** 受到光属性伤害加成 */
    BeLightUp,

    /** 伤害减免、被伤害增加 */
    Reduction,

    /** 追打 */
    ExtraAttack,

    /** 反击 */
    CounterAttack,

    /** 敌人行动完成后 */
    AfterEnemyAction,
    
    /** 增加暴击 */
    AddCritical,
}